﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TestApplication.business.src;

namespace TestApplication.usercontrols
{
    /// <summary>
    /// Interaction logic for ButtonBar.xaml
    /// </summary>

    public partial class ButtonBarControl : UserControl
    {
        private int numberOfButtons = 3;
        private Button[] buttons;
        private WebcamControl parent;

        public ButtonBarControl(WebcamControl element)
        {
            InitializeComponent();
            this.parent = element;
            this.Name = "buttonBar";
            int i;

            buttons = new Button[numberOfButtons];
            double buttonSpace = parent.WebcamImage.ActualWidth / numberOfButtons;

            string[] buttonNames = { "Start", "Stop", "Back" };
            
            for (i = 0; i < numberOfButtons; i++)
            {
                buttons[i] = new Button();
                buttons[i].FontSize = 28;
                buttons[i].FontWeight = FontWeights.Bold;
                buttons[i].Foreground = Brushes.RoyalBlue;
                buttons[i].Background = new ImageBrush(new BitmapImage(new Uri(@"resources\s1.png", UriKind.Relative)));
                buttons[i].Content = buttonNames[i];
                buttons[i].Width = (buttonSpace-0.1*buttonSpace);
                buttons[i].Height = 60 - 10;
                buttons[i].Margin = new Thickness(0.05*buttonSpace, 10, 0.05*buttonSpace, 0);
                buttons[i].Click += new RoutedEventHandler(ButtonBar_Click);
                buttonStack.Children.Add(buttons[i]);
            }
        }

        void ButtonBar_Click(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(buttons[0]))
                CaptureModel.Instance.movementBrain_Event(this, new System.ComponentModel.PropertyChangedEventArgs("BUTTON,0"));
            else if (sender.Equals(buttons[1]))
                CaptureModel.Instance.movementBrain_Event(this, new System.ComponentModel.PropertyChangedEventArgs("BUTTON,1"));
            else if (sender.Equals(buttons[2]))
                CaptureModel.Instance.movementBrain_Event(this, new System.ComponentModel.PropertyChangedEventArgs("BUTTON,2"));
        }

        public double[,] getButtonPositions()
        {
            double[,] pos = new double[numberOfButtons, 4];
            int i;

            for (i = 0; i < buttons.Length; i++)
            {
                pos[i, 0] = buttons[i].TranslatePoint(new Point(0, 0), buttonStack).X;
                pos[i, 1] = buttons[i].TranslatePoint(new Point(0, 0), buttonStack).Y;
                pos[i, 2] = buttons[i].Width + pos[i, 0];
                pos[i, 3] = buttons[i].Height + pos[i, 1];
            }

            return pos;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            CaptureModel.Instance.InitMovementBrain();
        }
    }
}
